# MySQL MCP Server Makefile
# 提供常用的开发、测试和部署命令

.PHONY: help install install-dev test test-unit test-integration test-coverage clean format lint type-check run build docs docker-build docker-run

# 默认目标
help:
	@echo "MySQL MCP Server - 可用命令:"
	@echo ""
	@echo "安装和设置:"
	@echo "  install      - 安装项目依赖"
	@echo "  install-dev  - 安装开发依赖"
	@echo ""
	@echo "测试:"
	@echo "  test         - 运行所有测试"
	@echo "  test-unit    - 运行单元测试"
	@echo "  test-integration - 运行集成测试"
	@echo "  test-coverage - 运行测试并生成覆盖率报告"
	@echo ""
	@echo "代码质量:"
	@echo "  format       - 格式化代码"
	@echo "  lint         - 代码风格检查"
	@echo "  type-check   - 类型检查"
	@echo ""
	@echo "运行:"
	@echo "  run          - 启动服务器"
	@echo "  run-dev      - 开发模式启动服务器"
	@echo ""
	@echo "构建和部署:"
	@echo "  build        - 构建项目"
	@echo "  clean        - 清理构建文件"
	@echo "  docs         - 生成文档"
	@echo ""
	@echo "Docker:"
	@echo "  docker-build - 构建Docker镜像"
	@echo "  docker-run   - 运行Docker容器"

# 安装依赖
install:
	@echo "安装项目依赖..."
	pip install -r requirements.txt

# 安装开发依赖
install-dev:
	@echo "安装开发依赖..."
	pip install -r requirements.txt
	pip install -e .
	pip install pytest pytest-cov pytest-asyncio black flake8 mypy

# 运行所有测试
test:
	@echo "运行所有测试..."
	pytest

# 运行单元测试
test-unit:
	@echo "运行单元测试..."
	pytest -m "not integration"

# 运行集成测试
test-integration:
	@echo "运行集成测试..."
	pytest -m integration

# 运行测试并生成覆盖率报告
test-coverage:
	@echo "运行测试并生成覆盖率报告..."
	pytest --cov=src/mysql_mcp_server --cov-report=html --cov-report=term-missing
	@echo "覆盖率报告已生成到 htmlcov/ 目录"

# 格式化代码
format:
	@echo "格式化代码..."
	black src/ tests/
	@echo "代码格式化完成"

# 代码风格检查
lint:
	@echo "进行代码风格检查..."
	flake8 src/ tests/ --max-line-length=88 --extend-ignore=E203,W503
	@echo "代码风格检查完成"

# 类型检查
type-check:
	@echo "进行类型检查..."
	mypy src/mysql_mcp_server/
	@echo "类型检查完成"

# 运行服务器
run:
	@echo "启动 MySQL MCP Server..."
	python -m src.mysql_mcp_server.main

# 开发模式运行服务器
run-dev:
	@echo "开发模式启动 MySQL MCP Server..."
	python -m src.mysql_mcp_server.main --config .env

# 构建项目
build:
	@echo "构建项目..."
	python setup.py sdist bdist_wheel
	@echo "构建完成，文件位于 dist/ 目录"

# 清理构建文件
clean:
	@echo "清理构建文件..."
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/
	rm -rf .pytest_cache/
	rm -rf htmlcov/
	rm -rf .coverage
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	@echo "清理完成"

# 生成文档
docs:
	@echo "生成文档..."
	mkdir -p docs/api
	pydoc-markdown > docs/api/README.md
	@echo "文档已生成到 docs/ 目录"

# 构建Docker镜像
docker-build:
	@echo "构建Docker镜像..."
	docker build -t mysql-mcp-server:latest .
	@echo "Docker镜像构建完成"

# 运行Docker容器
docker-run:
	@echo "运行Docker容器..."
	docker run -it --rm \
		-e DB_HOST=host.docker.internal \
		-e DB_PORT=3306 \
		-e DB_USER=root \
		-e DB_PASSWORD=password \
		-e DB_DATABASE=test \
		mysql-mcp-server:latest

# 代码质量检查（组合命令）
check: format lint type-check
	@echo "代码质量检查完成"

# 完整测试（组合命令）
test-all: check test-coverage
	@echo "完整测试完成"

# 发布准备（组合命令）
release-prep: clean check test-all build
	@echo "发布准备完成"

# 开发环境设置
dev-setup: install-dev
	@echo "复制环境变量模板..."
	@if [ ! -f .env ]; then cp .env.example .env; echo "请编辑 .env 文件配置数据库连接"; fi
	@echo "开发环境设置完成"

# 快速启动（用于演示）
demo:
	@echo "启动演示模式..."
	@echo "注意：请确保已配置 .env 文件"
	python -m src.mysql_mcp_server.main --config .env

# 性能测试
perf-test:
	@echo "运行性能测试..."
	pytest tests/ -k "performance" -v

# 安全测试
security-test:
	@echo "运行安全测试..."
	pytest tests/ -k "security" -v

# 生成需求文件
freeze:
	@echo "生成当前环境的需求文件..."
	pip freeze > requirements-frozen.txt
	@echo "需求文件已生成: requirements-frozen.txt"

# 检查依赖更新
check-deps:
	@echo "检查依赖更新..."
	pip list --outdated

# 更新依赖
update-deps:
	@echo "更新依赖..."
	pip install --upgrade -r requirements.txt

# 项目统计
stats:
	@echo "项目统计信息:"
	@echo "代码行数:"
	@find src/ -name "*.py" | xargs wc -l | tail -1
	@echo "测试行数:"
	@find tests/ -name "*.py" | xargs wc -l | tail -1
	@echo "文件数量:"
	@find src/ -name "*.py" | wc -l
	@echo "测试文件数量:"
	@find tests/ -name "*.py" | wc -l